import * as dotenv from 'dotenv';
import * as fs from 'fs';
import { Injectable } from '@nestjs/common';

@Injectable()
export class ConfigService {
  static get(): string {
    throw new Error('Method not implemented.');
  }
  private readonly envConfig;

  constructor() {
    const filePath =
      '.env' + (process.env.NODE_ENV ? '.' + process.env.NODE_ENV : '.develop');
    try {
      const stat = fs.statSync(filePath);
      if (stat && stat.isFile()) {
        this.envConfig = dotenv.parse(fs.readFileSync(filePath));
        // console.log(this.envConfig)
      } else {
      }
    } catch (e) {
      console.log(e);
    }
  }

  // Docker 变量或系统变量覆盖 env 配置文件
  get(key) {
    return process.env[key] || this.envConfig[key];
  }
}
